package com.xhej.rowreat.controller;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.xhej.rowreat.model.Bank;
import com.xhej.rowreat.model.Dimension;
import com.xhej.rowreat.model.ImportJson;
import com.xhej.rowreat.model.OutputJson;
import com.xhej.rowreat.model.Seat;
import com.xhej.rowreat.model.User;
import com.xhej.rowreat.service.RowSeatService;
import com.xhej.rowreat.util.Utils;

import net.sf.json.JSONArray;

@RestController
@RequestMapping("/rowseat")
public class RowSeatController {
	@Autowired
	private RowSeatService rowSeatService;
	
	@Autowired
	private List<Dimension> dimensionList;
	
	@Autowired
	private ImportJson importJson;
	
	@RequestMapping("/order")
	public @ResponseBody OutputJson order() throws Exception{
//		rowSeatService.readExcel();
		// 获取所有的机构对象
		List<Bank> listbank = new ArrayList<Bank>();
		List<Map<String,Object>> listbankMap = new ArrayList<Map<String,Object>>();
		listbankMap = Utils.readExcel("bank.xls");
		Bank bank = null;
		for(Map<String,Object> map:listbankMap){
			listbank.add(Utils.map2bean(map, Bank.class)); 
		} 
		this.importJson.setBanklist(listbank);
		// 获取所有的用户对象
//		List<User> listuser = new ArrayList<User>();
		List<Map<String,Object>> listUserMap = new ArrayList<Map<String,Object>>();
		listUserMap = Utils.readExcel("user.xls");
//		User user = null;
//		for(Map<String,Object> userMap:listUserMap){
//			user = Utils.map2bean(userMap, User.class);
//			listuser.add(user); 
//		}
		// 获取所有的维度以及维度计算公式
		List<User> listDimension = new ArrayList<User>();
		List<Map<String,Object>> listDimensionMap = new ArrayList<Map<String,Object>>();
		listDimensionMap = Utils.readExcel("dimension.xls");
		
		List<User> listUser2 = rowSeatService.orderUser2(listUserMap, listDimensionMap,listbankMap);
		
		/*String json = "[{\"row\":1,\"cols\":5},{\"row\":2,\"cols\":7},{\"row\":3,\"cols\":7},{\"row\":4,\"cols\":8}]";
		JSONArray jsonArray = JSONArray.fromObject(json);
		List<Seat> list = (List<Seat>) JSONArray.toCollection(jsonArray, Seat.class);
		String mjson = "[{\"row\":3,\"col\":3},{\"row\":4,\"col\":5}]";
		jsonArray = JSONArray.fromObject(mjson);
		List<Seat> mlist = (List<Seat>) JSONArray.toCollection(jsonArray, Seat.class);
		ImportJson importjson = new ImportJson();
		importjson.setList(list);
		importjson.setManualSeatList(mlist); */
//		List<Seat> listSeat = rowSeatService.orderSeat(importjson);
		List<Seat> listSeat = rowSeatService.orderSeat(importJson);
//		List<User> listUser = rowSeatService.orderUser(listUser2, dimensionList, importJson.getBanklist());
		return rowSeatService.optResult(listSeat, listUser2, importJson);
	}
	
	@RequestMapping("/input")
	public @ResponseBody ImportJson input() throws Exception{
		return importJson;
	}
}
